﻿using System;
using static Utility.WinAPI;
using System.Windows;

namespace WallpaperEngine.Core
{
    public class WallpaperWeb : WallpaperCore
    {
        Hook hook;

        public WallpaperWeb(Wallpaper wallpaper) : base(wallpaper)
        {
            try
            {
                System.Windows.Forms.WebBrowser Web = new System.Windows.Forms.WebBrowser()
                {
                    ScriptErrorsSuppressed = true,
                    IsWebBrowserContextMenuEnabled = false,
                    Width = _wallpaper.Fullscreen.Width,
                    Height = _wallpaper.Fullscreen.Height,
                    Url = new Uri(PathManager.Fullpath)
                };


                _wallpaper.Window = new System.Windows.Forms.Form()
                {
                    ShowInTaskbar = false,
                    FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
                };
                _wallpaper.Window.Controls.Add(Web);
                _wallpaper.Window.Show();

                SetParent(_wallpaper.Window.Handle, _wallpaper.Handle);
                SetWindowLong(_wallpaper.Window.Handle, WindowLongFlags.GWL_EXSTYLE, (int)WindowStylesEx.WS_EX_TOOLWINDOW);//WS_EX_TOOLWINDOW=128
                SetWindowPos(_wallpaper.Window.Handle, IntPtr.Zero, 0, 0, _wallpaper.Fullscreen.Width, _wallpaper.Fullscreen.Height, SetWindowPosFlags.FrameChanged);

                if (Configuration.Config.AllowMouseHook)
                {
                    Web.DocumentCompleted += ((s, e) =>
                    {
                        IntPtr target = IntPtr.Zero;
#if DEBUG
                        DateTime t = DateTime.Now;
                        while (target == IntPtr.Zero)
                        {
                            if ((DateTime.Now - t).Seconds > 3)
                            {
                                throw new Exception("网页壁纸启动失败!QAQ");
                            }
                            EnumChildWindows(_wallpaper.Handle, (handle, _) =>
                            {
                                var str = new System.Text.StringBuilder(30);
                                GetClassName(handle, str, 30);
                                if (str.ToString() == "Internet Explorer_Server")
                                {
                                    target = handle;
                                    return false;
                                }
                                return true;
                            }, IntPtr.Zero);
                        }
#else
                        EnumChildWindows(_wallpaper.Handle, (handle, _) =>
                        {
                            var str = new System.Text.StringBuilder(30);
                            GetClassName(handle, str, 30);
                            if (str.ToString() == "Internet Explorer_Server")
                            {
                                target = handle; return false;
                            }
                            return true;
                        }, IntPtr.Zero);
#endif
                        hook = new Hook(target);
                    });
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show($"{exception.Data.ToString()}\n{exception.Message}\n{exception.Source}\n{exception.StackTrace}\n{exception.TargetSite}\n初始化网页壁纸失败!\n请将本窗口截图发给作者,谢谢");
            }
        }

        ~WallpaperWeb()
        {
            if (hook != null)
            {
                hook.Stop();
            }
        }
    }
}
